//
//  PresentationController.swift
//  TakeTime
//
//  Created by bachelor on 16/11/12.
//  Copyright © 2016年 bachelor. All rights reserved.
//

import UIKit

class PresentationController: UIPresentationController {
    
    
    /// 遮罩视图
    lazy var dummyView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        
        return v
    }()
    
    private func prepareDummyView() {
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(PresentationController.close))
        dummyView.addGestureRecognizer(tap)
        
    }
    func close() {
        presentedViewController.dismiss(animated: true, completion: nil)
        
    }
    
    override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
        
        prepareDummyView()
    }
    
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        
        presentedView?.frame = CGRect.init(x: 100, y: 56, width: 200, height: 240)
        
        // 添加并且设置 dummyView
        dummyView.frame = containerView!.bounds
        containerView?.insertSubview(dummyView, at: 0)
        
        print(containerView?.frame)
//        // 弹出分组的大小布局
//        presentedView?.translatesAutoresizingMaskIntoConstraints = false
//        containerView?.addConstraint(NSLayoutConstraint.init(item: presentedView!, attribute: .centerX, relatedBy: .equal, toItem: containerView, attribute: .centerX, multiplier: 1.0, constant: 0))
//        containerView?.addConstraint(NSLayoutConstraint.init(item: presentedView!, attribute: .top, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1.0, constant: 56))
//        containerView?.addConstraint(NSLayoutConstraint.init(item: presentedView!, attribute: .bottom, relatedBy: .equal, toItem: containerView, attribute: .bottom, multiplier: 1.0, constant: -250))
    }
}
